package com.sll.hosptials.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sll.hosptials.controller.req.AddOrUpdateOrgInfoReq;
import com.sll.hosptials.controller.req.IdReq;
import com.sll.hosptials.controller.req.OrgInfoListReq;
import com.sll.hosptials.controller.req.OrgInfoPageReq;
import com.sll.hosptials.dto.OrgInfoDTO;
import com.sll.hosptials.controller.resp.OrgInfoPageResp;
import com.sll.hosptials.model.OrgInfo;
import com.sll.hosptials.service.OrgInfoService;
import com.sll.hosptials.mapper.OrgInfoMapper;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.sll.hosptials.config.ParamConfig.ENABLE;

/**
 * @author shaolulu
 * @description 针对表【org_info(组织信息)】的数据库操作Service实现
 * @createDate 2024-01-02 14:21:46
 */
@Service
public class OrgInfoServiceImpl extends ServiceImpl<OrgInfoMapper, OrgInfo> implements OrgInfoService {

    @Override
    public Boolean addOrgInfo(AddOrUpdateOrgInfoReq req) {
        if (ObjectUtil.isNotEmpty(req)) {
            OrgInfo orgInfo = BeanUtil.copyProperties(req, OrgInfo.class);
            this.saveOrUpdate(orgInfo);
        }
        return Boolean.TRUE;
    }

    @Override
    public Page<OrgInfoPageResp> orgInfoPage(OrgInfoPageReq req) {
        LambdaQueryWrapper<OrgInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(req.getOrgName()),OrgInfo::getOrgName,req.getOrgName());
        queryWrapper.orderByAsc(OrgInfo::getCreateDateTime);
        IPage<OrgInfoPageResp> respIPage = this.page(new Page<>(req.getPageNo(), req.getPageSize()), queryWrapper)
                .convert(info -> {
                    return BeanUtil.copyProperties(info, OrgInfoPageResp.class);
                });
        return (Page<OrgInfoPageResp>) respIPage;
    }

    @Override
    public List<OrgInfoPageResp> getOrgInfoList(OrgInfoListReq req) {
        LambdaQueryWrapper<OrgInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrgInfo::getEnable, ENABLE);
        List<OrgInfoPageResp> list = BeanUtil.copyToList(this.list(queryWrapper), OrgInfoPageResp.class);

        return list;
    }

//    void test(){
//        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
//// 自定义属性名 都要默认值的
//        treeNodeConfig.setWeightKey("order");
//        treeNodeConfig.setIdKey("rid");
//// 最大递归深度
//        treeNodeConfig.setDeep(3);
//
////转换器
//        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
//                (treeNode, tree) -> {
//                    tree.setId(treeNode.getId());
//                    tree.setParentId(treeNode.getParentId());
//                    tree.setWeight(treeNode.getWeight());
//                    tree.setName(treeNode.getName());
//                    // 扩展属性 ...
//                    tree.putExtra("extraField", 666);
//                    tree.putExtra("other", new Object());
//                });
//    }

    @Override
    public OrgInfoDTO orgInfoDetail(Long orgId) {
        if (ObjectUtil.isNotEmpty(orgId)) {
            OrgInfo orgInfo = this.getById(orgId);
            return BeanUtil.copyProperties(orgInfo, OrgInfoDTO.class);
        }
        return null;
    }

    @Override
    public Boolean delete(IdReq req) {
        if (CollUtil.isNotEmpty(req.getIds())) {
            return this.removeBatchByIds(req.getIds());
        }
        if (ObjectUtil.isNotEmpty(req.getId())) {
            return this.removeById(req.getId());
        }
        return Boolean.TRUE;
    }
}




